package temp;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import temp.casia.isiteam.zrb.hbase.client.ProjectHbaseClient;
import temp.casia.isiteam.zrb.hbase.client.bean.HBProjectAttachment;
import temp.casia.isiteam.zrb.hbase.client.bean.HBProjectInfo;
import temp.model.information.ProjectInfo;
import temp.model.information.dto.ProjectInfoDto;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hanhai.zrb.api.biz.service.CommonCounterService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.api.util.DateUtil;
import com.hanhai.zrb.api.util.NumberUtil;
import com.hanhai.zrb.model.project.UserProjectRef;
import com.hanhai.zrb.model.project.enums.ProjectStatusEnums;
import com.hanhai.zrb.model.project.vo.MyProjectInfoVo;



//@Service("projectInfoService")
@Deprecated
public class ProjectInfoServiceImpl implements ProjectInfoService{
	private static final Logger LOGGER = LoggerFactory.getLogger(ProjectInfoServiceImpl.class);
	@Resource
	private ProjectInfoMapper projectInfoMapper;
	
	@Resource
	private UserProjectRefService userProjectRefService;
	
	@Resource
	private CommonCounterService commonCounterService;
		
	
	@Resource
	private UserStatisticDetailService userCollectionService;

	@Override
	public ProjectInfo getProByAutoId(int id) {
		return projectInfoMapper.selectByPrimaryKey(id);
	}
	
	
	@Override
	public ProjectInfoVo getProVoByAutoId(int id) {
		ProjectInfoVo projectInfoVo = projectInfoMapper.selectVoByPrimaryKey(id);
		
		if( null != projectInfoVo){
			if(null!=projectInfoVo.getTradeDate()){
				projectInfoVo.setTradeDateStr(DateUtil.dateToString(projectInfoVo.getTradeDate()));
			}
			
			projectInfoVo.setArea(NumberUtil.roundFloat(projectInfoVo.getArea()));	
			
			//Hbase 扩展字段
			ProjectHbaseClient client = new ProjectHbaseClient();
			
			String provincePostcode = projectInfoVo.getProvincePostcode();
			String cityPostcode = projectInfoVo.getCityPostcode();
			String countyPostcode = projectInfoVo.getCountyPostcode();
			
			HBProjectInfo ci = client.getProjectInfoBasic(provincePostcode, cityPostcode, countyPostcode, id);
			if(ci == null){
				return projectInfoVo;
			}
			
//			if(StringUtils.isNotBlank(ci.getArea())&&StringUtils.isNumeric(ci.getArea())){
//				projectInfoVo.setArea(Float.valueOf(ci.getArea()));
//			}
//			private String assetType;// 资产类型，房屋、土地、交通工具、股权    
//			projectInfoVo.setAssetType(ci.getAssetType());
			projectInfoVo.setAssetIndex(ci.getAssetIndex());
			projectInfoVo.setBuilDensity(ci.getBuilDensity());
			projectInfoVo.setBusiness(ci.getBusiness());
			projectInfoVo.setDeposit(ci.getDeposit());
			projectInfoVo.setFinance(ci.getFinance());
			projectInfoVo.setGcjs(ci.getGcjs());
			projectInfoVo.setGcjsMap(JSONObject.toJavaObject(JSONObject.parseObject(ci.getGcjs()), Map.class));
			projectInfoVo.setGdsj(ci.getGdsj());
			projectInfoVo.setGeoCoor(ci.getGeoCoor());
			projectInfoVo.setGreeningRate(ci.getGreeningRate());
			projectInfoVo.setHospital(ci.getHospital());
			projectInfoVo.setPtPicPC(ci.getPt_pic());
			
			
//			if(StringUtils.isNotBlank(ci.getIndustry())){
//				projectInfoVo.setIndustryId(Integer.valueOf(ci.getIndustry()));
//			}
//			if(StringUtils.isNotBlank(ci.getIsValidate())){
//				projectInfoVo.setIsValidate(Integer.valueOf(ci.getIsValidate()));
//			}
			projectInfoVo.setJyxsynx(ci.getJyxsynx());
			projectInfoVo.setLandValue(ci.getLandValue());
			projectInfoVo.setLocation(ci.getLocation());
			projectInfoVo.setLocationPic(ci.getLocation_pic());
			projectInfoVo.setLocSummary(ci.getLocSummary());
			projectInfoVo.setMaxHeight(ci.getMaxHeight());
			projectInfoVo.setOtherCon(ci.getOtherCon());
			projectInfoVo.setPark(ci.getPark());
			projectInfoVo.setPayment(ci.getPayment());
			projectInfoVo.setPlotRatio(ci.getPlotRatio());
			projectInfoVo.setPolicy(ci.getPolicy());
			projectInfoVo.setPtPic(ci.getPt_pic());
			projectInfoVo.setPurpose(ci.getPurpose());
			projectInfoVo.setReserve(ci.getReserve());
			projectInfoVo.setScfy(ci.getScfy());
			projectInfoVo.setSchool(ci.getSchool());
			projectInfoVo.setSzb(ci.getSzb());
			projectInfoVo.setSzpt(ci.getSzpt());
			projectInfoVo.setTrade(ci.getTrade());
			projectInfoVo.setTraffic(ci.getTraffic());
			projectInfoVo.setTzhbhsy(ci.getTzhbhsy());
			projectInfoVo.setUnitPrice(ci.getUnitPrice());
			projectInfoVo.setZbdj(ci.getZbdj());
			projectInfoVo.setZbfj(ci.getZbfj());
			projectInfoVo.setZrdp(ci.getZrdp());
			List<String> list = new ArrayList();
			List<HBProjectAttachment> attachList = client.getProjectAttach(provincePostcode, cityPostcode, countyPostcode, projectInfoVo.getAutoId());
			if(null!=attachList){
				for(HBProjectAttachment attach:attachList){
					list.add(attach.getName());
				}
			}
			projectInfoVo.setAttachments(list);
			
			projectInfoVo.setType(ci.getType());
			projectInfoVo.setGcfy(ci.getGcfy());
			//client.closeClient();
			
		}else{
			projectInfoVo = new  ProjectInfoVo();
		}
		return projectInfoVo;
	}
	@Override
	public PageInfo queryPageByCondition(ProjectInfoDto projectInfoDto){
		PageHelper.startPage(projectInfoDto.getPageNum(), projectInfoDto.getPageSize(), true);
		List<ProjectInfoVo> page = queryByCondition(projectInfoDto);
		PageInfo pageInfo = new PageInfo(page);
		return pageInfo;
	}
	
	@Override
	public List<ProjectInfoVo> queryByCondition(ProjectInfoDto dto) {
		// TODO Auto-generated method stub
		List<ProjectInfoVo> list = projectInfoMapper.queryByCondition(dto);
		return list;
	}
	
	
	@Override
	public ProjectInfoVo getPurposeAndAssetTypeByAutoId(ProjectInfoVo projectInfoVo) {
		//ProjectInfoVo projectInfoVo = projectInfoMapper.selectVoByPrimaryKey(id);
		if( null != projectInfoVo){
			//Hbase 扩展字段
			ProjectHbaseClient client = new ProjectHbaseClient();
			
			String provincePostcode = projectInfoVo.getProvincePostcode();
			String cityPostcode = projectInfoVo.getCityPostcode();
			String countyPostcode = projectInfoVo.getCountyPostcode();
			
			HBProjectInfo ci = client.getProjectInfoBasic(provincePostcode, cityPostcode, countyPostcode, projectInfoVo.getAutoId());
			//client.closeClient();
			//			private String assetType;// 资产类型，房屋、土地、交通工具、股权 
			if(ci !=null){
				projectInfoVo.setAssetType(ci.getAssetType());
				projectInfoVo.setPurpose(ci.getPurpose());
			}
		
		}
		return projectInfoVo;
	}
	
	

	@Override
	public void updateStatus(ProjectInfo record) {
		
		try {
			projectInfoMapper.updateByPrimaryKeySelective(record);
		} catch (Exception e) {
			LOGGER.error(" do projectInfoMapper.updateByPrimaryKeySelective(record) false userId={}",record.getUserId(),e);
		}
	}

	@Override
	public Map<Integer,ProjectInfoVo> queryByIdList(List<Integer> list) {
		List<ProjectInfoVo> projectInfoList = this.projectInfoMapper.queryByIdList(list);
		Map<Integer,ProjectInfoVo> map = new HashMap<Integer,ProjectInfoVo>();
		for(ProjectInfoVo vo:projectInfoList){
			map.put(vo.getAutoId(), vo);
		}
		return map;
	}

	@Override
	public int saveProject(ProjectInfo record) {
		
		
		int ret = 0;
		try {
			projectInfoMapper.insertSelective(record);
		} catch (Exception e) {
			LOGGER.error(" do projectInfoMapper.insertSelective(record) false userId={}",record.getUserId(),e);
			ret = 1;
		}
		
		return ret;
	}
	
	@Override
	public void updateProject(ProjectInfo record) {
		try {
			projectInfoMapper.updateByPrimaryKeySelective(record);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LOGGER.error(" do projectInfoMapper.updateByPrimaryKeySelective(record) false userId={}",record.getUserId(),e);
		}
	}

	@Override
	public List<MyProjectInfoVo> queryMyProjectList(Date insertTime,boolean forward,Integer userId,Integer limit) throws IllegalAccessException, InvocationTargetException {
		
		List<MyProjectInfoVo> myProjectList = new ArrayList<MyProjectInfoVo>();
		List<UserProjectRef> list = userProjectRefService.queryRefByCondition(insertTime, forward, userId, UserProjectRef.refProType.MY_PROJECT.getType(), limit);
		if(null!=list){
			for(UserProjectRef ref:list){
				ProjectInfo info = projectInfoMapper.selectByPrimaryKey(ref.getProjectId());
				if(info.getProjectStatus().equals(ProjectStatusEnums.RELEASE.getCode())){
//					Integer collectionNum = getCommonCount(UserStatisticDetail.Type.pro_col.getCountKey(),String.valueOf(ref.getProjectId()));;
				//	Integer commentNum = projectCommentMapper.getCommentCountByProjectId(ref.getProjectId());
				//	Integer inspectNum = projectInspectMapper.getInspectCountByProjectId(ref.getProjectId());
					MyProjectInfoVo myVo=new MyProjectInfoVo();
					myVo.setAutoId(ref.getAutoId());
					myVo.setInsertTime(ref.getInsertTime());
					myVo.setProjectId(ref.getProjectId());
					myVo.setUserId(ref.getUserId());
//					myVo.setCollectionNum(null==collectionNum?0:collectionNum);
					Integer commentNum = 0;
					Integer inspectNum = 0;
					myVo.setCommentNum(null==commentNum?0:commentNum);
					myVo.setInspectNum(null==inspectNum?0:inspectNum);
					myVo.setProjectName(info.getShortName());
					myVo.setIntro(info.getIntro());
					myVo.setPublishTime(info.getInsertTime());
					myVo.setPic(info.getPic());
					myProjectList.add(myVo);
				}
			}
		}
		return myProjectList;
	}
	
	
	

	@Override
	public List<ProjectInfo> getProjectByNameAndStatus(String name,
			Integer status) {
		return projectInfoMapper.getProjectByNameAndStatus(name, status);
	}
	
	@Override
	public List<ProjectInfoVo> getByIdList(List<Integer> list){
		return projectInfoMapper.queryByIdList(list);
	}
	public List<ProjectInfoVo> listAll(){
		return projectInfoMapper.listAll();
	}


	@Override
	public Integer queryCountByProName(String projectName) {
		return projectInfoMapper.queryCountByProName(projectName);
	}


	
	
	
	
//	private Integer getCommonCount(String mainKey,String secondaryKey){
//		CommonCounterKey commonCounterKey=new CommonCounterKey();
//		commonCounterKey.setMainKey(MainKeyType.PROJ_VIEW_COUNT.name());
//		commonCounterKey.setSecondaryKey(secondaryKey);
//		Long num = commonCounterService.getCurrentCountByKey(commonCounterKey);
//		if(null==num){
//			num=0L;
//		}
//		return Integer.valueOf(String.valueOf(num));
//	}

	
}



